package com.fzk.blog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzk.blog.bean.Blog;
import com.fzk.blog.bean.vo.BlogInfo;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author fzk
 * @date 2021-08-15 15:41
 */
public interface BlogMapper extends BaseMapper<Blog> {


    @Select("SELECT b.id,b.type_id typeId,b.recommend_level recommendLevel,b.create_flag createFlag,b.title,b.description,b.image_url imageUrl, " +
            "   b.view_times viewTimes,b.published,b.publish_date publishDate,t.name typeName,b.create_by createBy,b.edit_time editTime " +
            "   FROM tbl_blog b LEFT JOIN tbl_type t ON (b.type_id=t.id AND t.flag=0) " +
            "   WHERE b.flag=0 AND b.id=#{id}")
    BlogInfo getBlogInfoById(int id);

    @Update("update tbl_blog set flag=1,edit_by=#{loginId} where id=#{id} and flag=0")
    int logicalDelete(int id, int loginId);

    @Select("select content from tbl_blog where id=#{id} and flag=0")
    String getBlogContent(int id);

    @SelectProvider(BlogSqlProvider.class)//动态SQL1
    List<BlogInfo> getBlogInfoList(String title, String username, Integer typeId, Boolean published, Integer flag);

    @SelectProvider(BlogSqlProvider.class)//动态SQL2
    List<BlogInfo> getBlogInfoPage(String title, Integer typeId,Integer userId, Integer[] tagId, Boolean published,Integer flag, int index, int pageSize);

    @SelectProvider(BlogSqlProvider.class)//动态SQL3：和上面这个动态SQL2几乎一致，除了没有分页查询
    List<Integer> getBlogIdList(String title, Integer typeId,Integer userId, Integer[] tagId, Boolean published,Integer flag);

    @Select("select create_by from tbl_blog where id=#{id} and flag=0")
    Integer getCreateById(int id);

    @Select("select title,publish_date publishDate from tbl_blog where published=1 and flag=0 order by publishDate DESC")
    List<Blog> getBlogTimeLine();

    @Update("UPDATE tbl_blog SET view_times=view_times+1 WHERE id=#{id}")
    int increaseViewTimes(int id);

    @Select("SELECT view_times FROM tbl_blog WHERE id=#{id}")
    int getViewTimesById(int id);

    @Update("UPDATE tbl_blog SET flag=0,edit_by=#{loginId} WHERE id=#{id} AND flag=1")
    int recycleBlog(int id, int loginId);

    @Select("SELECT COUNT(*) FROM tbl_blog WHERE create_by=#{userId} AND flag=0 AND published=#{published}")
    int getNumberByUserId(int userId,boolean published);

    @Select("SELECT b.id,b.type_id typeId,b.recommend_level recommendLevel,b.create_flag createFlag,b.title,b.description,b.image_url imageUrl, " +
            "   b.view_times viewTimes,b.published,b.publish_date publishDate,t.name typeName,b.create_by createBy,b.edit_time editTime " +
            "   FROM tbl_blog b LEFT JOIN tbl_type t ON (b.type_id=t.id AND t.flag=0) " +
            "   WHERE b.flag=0 AND b.published=true " +
            "   ORDER BY b.publish_date DESC , b.edit_time DESC " +
            "   LIMIT 6")
    List<BlogInfo> getNewBlogInfoList();

}